package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.system.domain.SysMenu;
import com.ruoyi.system.domain.vo.TreeSelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.XcxMenuMapper;
import com.ruoyi.system.domain.XcxMenu;
import com.ruoyi.system.service.IXcxMenuService;

/**
 * 小程序菜单Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-29
 */
@Service
public class XcxMenuServiceImpl implements IXcxMenuService
{
    @Autowired
    private XcxMenuMapper xcxMenuMapper;


    @Override
    public List<Long> GetxcxRuot(Long id) {
        return xcxMenuMapper.GetxcxRuot(id);
    }

    /**
     * 查询小程序菜单
     *
     * @param xcxMenuId 小程序菜单主键
     * @return 小程序菜单
     */
    @Override
    public XcxMenu selectXcxMenuByXcxMenuId(Long xcxMenuId)
    {
        return xcxMenuMapper.selectXcxMenuByXcxMenuId(xcxMenuId);
    }

    /**
     * 查询小程序菜单列表
     *
     * @param xcxMenu 小程序菜单
     * @return 小程序菜单
     */
    @Override
    public List<XcxMenu> selectXcxMenuList(XcxMenu xcxMenu)
    {
        return xcxMenuMapper.selectXcxMenuList(xcxMenu);
    }

    /**
     * 新增小程序菜单
     *
     * @param xcxMenu 小程序菜单
     * @return 结果
     */
    @Override
    public int insertXcxMenu(XcxMenu xcxMenu)
    {
        return xcxMenuMapper.insertXcxMenu(xcxMenu);
    }

    /**
     * 修改小程序菜单
     *
     * @param xcxMenu 小程序菜单
     * @return 结果
     */
    @Override
    public int updateXcxMenu(XcxMenu xcxMenu)
    {
        return xcxMenuMapper.updateXcxMenu(xcxMenu);
    }

    /**
     * 批量删除小程序菜单
     *
     * @param xcxMenuIds 需要删除的小程序菜单主键
     * @return 结果
     */
    @Override
    public int deleteXcxMenuByXcxMenuIds(Long[] xcxMenuIds)
    {
        return xcxMenuMapper.deleteXcxMenuByXcxMenuIds(xcxMenuIds);
    }

    /**
     * 删除小程序菜单信息
     *
     * @param xcxMenuId 小程序菜单主键
     * @return 结果
     */
    @Override
    public int deleteXcxMenuByXcxMenuId(Long xcxMenuId)
    {
        return xcxMenuMapper.deleteXcxMenuByXcxMenuId(xcxMenuId);
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param xcxMenus 菜单列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildXcxMenuTreeSelect(List<XcxMenu> xcxMenus)
    {
        List<XcxMenu> menuTrees = buildXcxMenuTree(xcxMenus);
//        System.out.println("构造后"+menuTrees);
        return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 重载获取小程序下拉菜单
     *
     * @param userid 用户id
     * @return 下拉树结构列表
     */
    @Override
    public List<XcxMenu> selectXcxMenuList(Long userid) {
        return xcxMenuMapper.selectXcxMenuListByRoleId(new XcxMenu());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param xcxMenus 菜单列表
     * @return 树结构列表
     */
    @Override
    public List<XcxMenu> buildXcxMenuTree(List<XcxMenu> xcxMenus)
    {
        List<XcxMenu> returnList = new ArrayList<XcxMenu>();
        List<Long> tempList = xcxMenus.stream().map(XcxMenu::getXcxMenuId).collect(Collectors.toList());
        for (Iterator<XcxMenu> iterator = xcxMenus.iterator(); iterator.hasNext();)
        {
            XcxMenu xcxMenu = (XcxMenu) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(xcxMenu.getXcxMenuTopid()))
            {
                recursionFn(xcxMenus, xcxMenu);
                returnList.add(xcxMenu);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = xcxMenus;
        }
        return returnList;
    }
    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<XcxMenu> list, XcxMenu t)
    {
        // 得到子节点列表
        List<XcxMenu> childList = getChildList(list, t);
        t.setChildren(childList);
        for (XcxMenu tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<XcxMenu> getChildList(List<XcxMenu> list, XcxMenu t)
    {
        List<XcxMenu> tlist = new ArrayList<XcxMenu>();
        Iterator<XcxMenu> it = list.iterator();
        while (it.hasNext())
        {
            XcxMenu n = (XcxMenu) it.next();
            if (n.getXcxMenuTopid().longValue() == t.getXcxMenuId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<XcxMenu> list, XcxMenu t)
    {
        return getChildList(list, t).size() > 0;
    }


    /**
     * 根据角色ID查询菜单权限
     *
     * @param roleId 角色ID
     * @return 结果
     */
    @Override
    public List<XcxMenu> selectXcxMenuListS(String roleId) {
        String [] rid= roleId.split(",");
        Long[] num = new Long[rid.length];
        for (int idx = 0; idx < rid.length; idx++) {
            num[idx] = Long.parseLong(rid[idx]);
        }
        return xcxMenuMapper.selectXcxMenuListS(num);
    }
}
